<template>
  <j-modal :title="title" :width="1000" :visible="visible" switchFullscreen @cancel="handleCancel" :footer="null">
    <a-spin :spinning="loading">
      <a-form-model :model="form" ref="form" :rules="rules" :label-col="{ span: 8 }" :wrapper-col="{ span: 14 }">
        <a-row>
          <a-col :span="11">
            <a-form-model-item label="指标名称" required prop="indicatorName">
              <a-input v-model="form.indicatorName" :readOnly="isDisabled" :placeholder="compPlaceholderInput">
              </a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <a-form-model-item label="上级目录/上级指标" prop="seniorIndicatorName">
              <a-input
                v-model="form.seniorIndicatorName"
                :readOnly="isDisabled"
                :disabled="showType === '1'"
                :placeholder="compPlaceholderInput"
              >
              </a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="11">
            <a-form-model-item label="指标类型" required prop="indicatorType">
              <a-input
                v-if="isDisabled"
                :value="$filterDictTextByCache('indicatorType', form.indicatorType)"
                readOnly
              ></a-input>
              <a-select
                v-else
                v-model="form.indicatorType"
                :placeholder="compPlaceholderSel"
                style="width: 100%"
                allowClear
              >
                <a-select-option v-for="(item, key) in indexTypeOpts" :key="key" :value="item.value"
                  >{{ item.title }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <a-form-model-item label="加/减分基数（P）" required prop="addCardinalNumber">
              <a-input v-model="form.addCardinalNumber" :readOnly="isDisabled" :placeholder="compPlaceholderInput">
              </a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="11">
            <a-form-model-item label="任务量（Y）" prop="indexY">
              <a-input v-model="form.taskNumber" :readOnly="isDisabled" :placeholder="compPlaceholderInput"> </a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <a-form-model-item label="基 数（Z）" required prop="cardinalNumber">
              <a-input v-model="form.cardinalNumber" :readOnly="isDisabled" :placeholder="compPlaceholderInput">
              </a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="11">
            <a-form-model-item label="是否可修正得分" prop="xzdf">
              <a-input
                v-if="isDisabled"
                :value="$filterDictTextByCache('yn', form.isScoreCorrection)"
                readOnly
              ></a-input>
              <a-select
                v-else
                v-model="form.isScoreCorrection"
                :placeholder="compPlaceholderSel"
                style="width: 100%"
                allowClear
              >
                <a-select-option v-for="(item, key) in ynOpts" :key="key" :value="item.value"
                  >{{ item.title }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <a-form-model-item label="是否与上级共享评分" prop="gxpf">
              <a-input v-if="isDisabled" :value="$filterDictTextByCache('yn', form.isSharedScore)" readOnly></a-input>
              <a-select
                v-else
                v-model="form.isSharedScore"
                :placeholder="compPlaceholderSel"
                style="width: 100%"
                allowClear
              >
                <a-select-option v-for="(item, key) in ynOpts" :key="key" :value="item.value"
                  >{{ item.title }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="11">
            <a-form-model-item label="上限分值" prop="sxfz">
              <a-input v-model="form.upperScore" :readOnly="isDisabled" :placeholder="compPlaceholderInput"> </a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <a-form-model-item label="下限分值" prop="xxfz">
              <a-input v-model="form.lowerScore" :readOnly="isDisabled" :placeholder="compPlaceholderInput"> </a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="11">
            <a-form-model-item label="权重 %（M）" prop="qzPer">
              <a-input v-model="form.weight" :readOnly="isDisabled" :placeholder="compPlaceholderInput"> </a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <a-form-model-item label="是否禁用" prop="dis">
              <a-input v-if="isDisabled" :value="$filterDictTextByCache('yn', form.isDisable)" readOnly></a-input>
              <a-select
                v-else
                v-model="form.isDisable"
                :placeholder="compPlaceholderSel"
                style="width: 100%"
                allowClear
              >
                <a-select-option v-for="(item, key) in ynOpts" :key="key" :value="item.value"
                  >{{ item.title }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="11">
            <a-form-model-item label="是否末级" prop="end">
              <a-input v-if="isDisabled" :value="$filterDictTextByCache('yn', form.collectWay)" readOnly></a-input>
              <a-select
                v-else
                v-model="form.collectWay"
                :placeholder="compPlaceholderSel"
                style="width: 100%"
                allowClear
              >
                <a-select-option v-for="(item, key) in ynOpts" :key="key" :value="item.value"
                  >{{ item.title }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <a-form-model-item label="顺 序" prop="order">
              <a-input v-model="form.sort" :readOnly="isDisabled" :placeholder="compPlaceholderInput"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="11">
            <a-form-model-item label="初评分值（N）">
              <a-input v-model="form.firstScore" :readOnly="isDisabled" :placeholder="compPlaceholderInput"> </a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <a-form-model-item label="完成量（L）" prop="incidentName">
              <a-input v-model="form.finishNumber" :readOnly="isDisabled" :placeholder="compPlaceholderInput">
              </a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="11">
            <a-form-model-item label="是否允许分发" prop="distribute">
              <a-input v-if="isDisabled" :value="$filterDictTextByCache('yn', form.isDistribution)" readOnly></a-input>
              <a-select
                v-else
                v-model="form.isDistribution"
                :placeholder="compPlaceholderSel"
                style="width: 100%"
                allowClear
              >
                <a-select-option v-for="(item, key) in ynOpts" :key="key" :value="item.value"
                  >{{ item.title }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <a-form-model-item label="采集方式" prop="collectMode">
              <a-input
                v-if="isDisabled"
                :value="$filterDictTextByCache('collectionMethod', form.collectionMethod)"
                readOnly
              ></a-input>
              <a-select
                v-else
                v-model="form.collectionMethod"
                :placeholder="compPlaceholderSel"
                style="width: 100%"
                allowClear
              >
                <a-select-option v-for="(item, key) in collectModeOpts" :key="key" :value="item.value"
                  >{{ item.title }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="11">
            <a-form-model-item label="考核周期" required prop="assessmentCycle">
              <a-input
                v-if="isDisabled"
                :value="$filterDictTextByCache('assessmentCycle', form.assessmentCycle)"
                readOnly
              ></a-input>
              <a-select
                v-else
                v-model="form.assessmentCycle"
                placeholder="请选择"
                style="width: 100%"
                allowClear
              >
                <a-select-option v-for="(item, key) in assessmentCycle" :key="key" :value="item.value"
                >{{ item.title }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="11" class="col-2">
            <a-form-model-item label="考核标准" prop="khbz">
              <a-textarea
                v-model="form.standardAssessment"
                :readOnly="isDisabled"
                :rows="3"
                :placeholder="
                  !isDisabled
                    ? '任务量-Y，基数-Z，加减分基数-P，权重-M，完成量-L，初评分值-N\n当L>=Y时，N=Z*M\n当L<Y时，N=(Z+P*L)*M'
                    : ''
                "
              ></a-textarea>
            </a-form-model-item>
          </a-col>
        </a-row>
        <p class="dotted"></p>
        <a-row>
          <a-col :span="11">
            <a-form-model-item label="审核机构" prop="auditOrg">
              <a-input v-if="isDisabled" :value="form.auditOrganName" readOnly></a-input>
              <a-select
                v-else
                v-model="form.auditOrgan"
                :placeholder="compPlaceholderSel"
                style="width: 100%"
                allowClear
                @change="changeOrgs"
              >
                <a-select-option v-for="(item, index) in auditOrgOpts" :key="index" :value="item.id"
                  >{{ item.departName }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <a-form-model-item label="审核部门" prop="auditDep">
              <a-input v-if="isDisabled" :value="form.auditDepartName" readOnly></a-input>
              <a-select
                v-else
                v-model="form.auditDepart"
                :placeholder="compPlaceholderSel"
                style="width: 100%"
                allowClear
                @change="changeDeps"
              >
                <a-select-option v-for="item in auditDepOpts" :key="item.id">{{ item.departName }} </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="11">
            <a-form-model-item label="审核人" prop="checker">
              <a-input v-if="isDisabled" :value="form.auditPeopleName" readOnly></a-input>
              <a-select
                v-else
                v-model="form.auditPeople"
                :placeholder="compPlaceholderSel"
                style="width: 100%"
                allowClear
              >
                <a-select-option v-for="item in checkerOpts" :key="item.id">{{ item.username }} </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
      <div class="footerBox" v-if="showType === '4'">
        <a-button class="btn" @click="handleAudit(0)">拒 绝</a-button>
        <a-button class="btn" @click="handleAudit(1)" style="margin-left: 8px" type="primary">通 过</a-button>
      </div>
      <div class="footerBox" v-else-if="showType !== '3'">
        <a-button class="btn" @click="handleCancel">取 消</a-button>
        <a-button class="btn" @click="handleOk" style="margin-left: 8px" type="primary">保 存</a-button>
      </div>
    </a-spin>
  </j-modal>
</template>

<script>
import Api from '@/api/sjsk/performanceService'
import { getDictItemsFromCache } from '@/api/api'

export default {
  name: 'AddIndex',
  components: {},
  props: {
    showType: String,
    checkRow: Object,
    visible: Boolean,
  },
  data() {
    return {
      loading: false,
      isDisabled: false,
      title: null,
      form: {
        isScoreCorrection:'1',
        isSharedScore:'0',
        isDisable:'0',
        collectWay:'1',
      },
      rules: {
        indicatorName: [{ required: true, message: '请输入指标名称' }],
        indicatorType: [{ required: true, message: '请选择指标类型' }],
        addCardinalNumber: [{ required: true, message: '请输入加/减分基数' }],
        cardinalNumber: [{ required: true, message: '请输入基数' }],
        assessmentCycle: [{ required: true, message: '请选择考核周期' }],
      },
      indexTypeOpts: getDictItemsFromCache('indicatorType'),
      ynOpts: getDictItemsFromCache('yn'),
      collectModeOpts: getDictItemsFromCache('collectionMethod'),
      assessmentCycle:getDictItemsFromCache('assessmentCycle'),
      auditOrgOpts: [],
      auditDepOpts: [],
      checkerOpts: [],
    }
  },
  computed: {
    compPlaceholderInput() {
      return this.showType !== '3' ? '请输入' : ''
    },
    compPlaceholderSel() {
      return this.showType !== '3' ? '请选择' : ''
    },
  },
  async created() {
    const type = this.showType
    if (type === '1') {
      this.title = '新增指标'
    } else if (type === '2') {
      this.title = '编辑指标'
    } else if (type === '3') {
      this.title = '指标详情'
    } else if (type === '4') {
      this.title = '指标审核'
    }
    if (type === '1') {
      this.form.seniorIndicator = this.checkRow.seniorIndicator
      this.form.seniorIndicatorName = this.checkRow.indicatorName
      this.getAuditOrgs()
    } else {
      if (type !== '3') {
        await this.getAuditOptsData(this.checkRow)
      }
      this.getDetails(this.checkRow.id)
      // this.form = this.checkRow
    }
    this.isDisabled = type === '3'
  },
  mounted() {},

  methods: {
    // 获取指标详情
    async getDetails(_id) {
      this.loading = true
      await Api.getIndicatorDetails(_id)
        .then((res) => {
          if (res.success) {
            this.form = res.result
          } else {
            this.$message.error(res.message)
          }
        })
        .finally(() => {
          this.loading = false
        })
    },

    // 获取审核机构
    async getAuditOrgs() {
      await Api.queryDepartList({ departType: 2, orgTypeExt: 2 }).then((res) => {
        if (res.success) {
          this.auditOrgOpts = res.result
        } else {
          this.$message.error(res.message)
        }
      })
    },
    // 获取审核部门
    async changeOrgs(_id) {
      this.auditDepOpts = []
      await Api.queryDepartList({ departType: 2, orgTypeExt: 1, parentDepartId: _id }).then((res) => {
        if (res.success) {
          this.auditDepOpts = res.result
        } else {
          this.$message.error(res.message)
        }
      })
    },
    // 获取审核人
    async changeDeps(_id) {
      this.checkerOpts = []
      await Api.getUsersByDepartId({ id: _id }).then((res) => {
        if (res.success) {
          this.checkerOpts = res.result
        } else {
          this.$message.error(res.message)
        }
      })
    },
    // 获取审核信息
    async getAuditOptsData(_checkRow) {
      this.loading = true
      await this.getAuditOrgs()
      _checkRow.auditOrgan && (await this.changeOrgs(_checkRow.auditOrgan))
      _checkRow.auditDepart && (await this.changeDeps(_checkRow.auditDepart))
      this.loading = false
    },

    // 保存
    handleOk() {
      const that = this
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.loading = true
          const methodApi = this.form.id ? 'updateIndicatorById' : 'addIndicator'
          this.form.contentIndicator = 0
          Api[methodApi](this.form)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.error(res.message)
              }
            })
            .finally(() => {
              that.loading = false
              that.handleCancel()
            })
        } else {
          return false
        }
      })
    },

    // 审核
    handleAudit(_status) {
      const that = this
      // this.$refs.form.validate((valid) => {
      //   if (valid) {
      that.loading = true
      Api.auditIndicatorById({ id: this.form.id, auditStatus: _status })
        .then((res) => {
          if (res.success) {
            that.$message.success(_status == 1 ? '通过成功' : '拒绝成功')
            that.$emit('ok')
          } else {
            that.$message.error(res.message)
          }
        })
        .finally(() => {
          that.loading = false
          that.handleCancel()
        })
      // } else {
      //   return false
      // }
      // })
    },

    handleCancel() {
      this.$emit('update:visible', false)
    },
  },
}
</script>

<style lang="less" scoped>
.col-2 {
  width: 100%;
  ::v-deep .ant-form-item-label {
    width: 15%;
  }
  ::v-deep .ant-form-item-control-wrapper {
    width: 73%;
  }
}
.dotted {
  border-top: 1px dotted #e9e9e9;
}
.footerBox {
  display: flex;
  justify-content: flex-end;
  width: 100%;
  padding-top: 20px;
  border-top: 1px solid #e8e8e8;
}
::v-deep .ant-form {
  height: 70vh;
  overflow-y: scroll;
}
</style>
